package com.elepro.shoppingmall.service.user;

import com.elepro.shoppingmall.entity.User;
import com.elepro.shoppingmall.mapper.UserMapper;
import net.bytebuddy.utility.RandomString;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private BCryptPasswordEncoder encoder;
    @Resource
    private JavaMailSenderImpl mailSender;
    @Resource
    private RedisTemplate<String, String> redisTemplate;

    //通过username查找用户信息
    @Override
    public User getUserByUsername(String username) {
        return userMapper.getUserByUsername(username);
    }

    //通过username查找用户权限
    @Override
    public List<User> getAuthorityByUsername(String username) {
        return userMapper.getAuthorityByUsername(username);
    }

    //增加用户
    @Override
    public int insertUser(User user, String code) {
        //验证验证码
        if (!code.equals(redisTemplate.opsForValue().get(user.getEmail()))) {
            return 0;
        }
        try {
            //加密
            user.setPassword(encoder.encode(user.getPassword()));
            user.setValid(1);
            user.setAuthority("ROLE_common");
            user.setUserMoney(0);
            return userMapper.insertUser(user);

        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int adminInsertUser(User user) {
        try {
            //加密
            user.setPassword(encoder.encode(user.getPassword()));
            user.setValid(1);
            return userMapper.insertUser(user);

        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    //删除用户
    @Override
    public int deleteUserById(long id) {
        try {
            userMapper.deleteUserById(id);
            userMapper.deleteOrderByUserId(id);
            userMapper.deleteMessageByUserId(id);
            return userMapper.deleteUserMessageByUserId(id);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    //修改用户
    @Override
    public int updateUser(User user) {
        try {
            return userMapper.updateUser(user);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    //查询用户
    @Override
    public List<User> getAllUser() {
        return userMapper.getAllUser();
    }

    @Override
    public User getUserById(long id) {
        return userMapper.getUserById(id);
    }

    @Override
    public User getUserByName(String username) {
        return userMapper.getUserByName(username);
    }

    //邮箱获取验证码
    @RabbitListener(queues = "guo_queue_email")
    @Override
    public void sendEmailCode(String email) {
        //生成随机字符串
        String code = RandomString.make(6);
        //存入缓存中
        redisTemplate.opsForValue().set(email, code, 30, TimeUnit.MINUTES);

        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setFrom("guo13267725770@163.com");
        mailMessage.setTo(email);
        mailMessage.setSubject("登录验证码");
        mailMessage.setText("您的登录验证码: " + code);
        try {
            mailSender.send(mailMessage);
            System.out.println("验证码发送成功");
        } catch (MailException e) {
            System.out.println("邮件发送失败：" + e.getMessage());
            e.printStackTrace();
        }
    }
}
